package com.eattravel.controller;

import com.eattravel.pojo.User;
import com.eattravel.service.UserService;
import com.eattravel.vo.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * zx
 * 关于用户的操作
 * 最后修改时间 9.19
 */
@Controller
@RequestMapping("user")

public class UserController {
    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(5);
        System.out.println(atomicInteger.compareAndSet(5, 2019)+"\t current"+atomicInteger.get());
        System.out.println(atomicInteger.compareAndSet(5, 2014)+"\t current"+atomicInteger.get());

    }

    @Autowired
    UserService userService;


    /**
     * 用户登录注册模块
     * @param session
     * @param userName
     * @param userPwd
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public ResultData login(HttpSession session, String userName, String userPwd){


        User userinfo = userService.login(userName,userPwd);
        ResultData  resultData = new ResultData();
        if(null == userinfo) {
            resultData.setCode(1);
            resultData.setMessage("账号或密码错误");
        } else {
            session.setAttribute("userName",userName);
            resultData.setCode(0);
            resultData.setMessage("成功登陆");
//            resultData.setData(userinfo);
        }
        return resultData;
    }

    /**
     * 查询单个对象
     * 将用户名字保存到session中
     * @param session
     * @return
     */
    @RequestMapping("finduser")
    @ResponseBody
    public String  FindUser(HttpSession session){

        String userName = (String) session.getAttribute("userName");

        return userName;


    }

    /**
     * 用户退出
     * @param session
     * @return
     */
    @RequestMapping("exitServlet")
    public String exit(HttpSession session){
        session.invalidate();

        return "redirect:http://localhost:8080/login.html";
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping("registerUser")
    @ResponseBody
    public ResultData RegistUser(User user){

        ResultData  resultData = new ResultData();

        boolean flag=userService.regist(user);
        if(flag){
            //注册成功
            resultData.setCode(0);
            resultData.setMessage("注册成功");

        }else {
            resultData.setCode(1);
            resultData.setMessage("用户已经存在");

        }
        return  resultData;
}

    /**
     * 根据用户的Id查询用户的信息
     * @param userId
     * @return
     */
    @RequestMapping("userInfo")
    @ResponseBody
    public User userInfo(String userId) {

        User user=userService.findUserByUserId(userId);

        return user;

    }


    @RequestMapping("updataUserInfo")
    @ResponseBody
    public ResultData updataUserInfo(User user){
        System.out.println(user);
        int count=userService.updateUserInfo(user);
        ResultData resultData=new ResultData();
        if(count>0){

            resultData.setCode(0);
            resultData.setMessage("修改信息成功");
            return resultData;

        }
        resultData.setCode(1);
        resultData.setMessage("修改信息失败 请重试");

        return resultData;
    }



}
